package chenjun.mydatabindingdemo.fragment;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import chenjun.mydatabindingdemo.BR;
import chenjun.mydatabindingdemo.R;
import chenjun.mydatabindingdemo.activity.ImageActivity;
import chenjun.mydatabindingdemo.adapter.LvAdapter;
import chenjun.mydatabindingdemo.bean.JokeImgBean;
import chenjun.mydatabindingdemo.constant.StringUrl;
import chenjun.mydatabindingdemo.model.HttpUtil;
import chenjun.mydatabindingdemo.util.DataChange;
import chenjun.mydatabindingdemo.util.Logger;
import chenjun.mydatabindingdemo.view.OnShow;

/**
 * A simple {@link Fragment} subclass.
 */
public class JokeImgLastFragment extends Fragment implements OnShow {

    private ListView lvImg;
    private HttpUtil httpUtil;
    private Context mContext;
    private List<JokeImgBean.ResultBean.DataBean> mList;
    private LvAdapter<JokeImgBean.ResultBean.DataBean> adapter;
    private boolean hasShow;
    private int index;
    private View footview;

    public static JokeImgLastFragment newInstance() {

        Bundle args = new Bundle();

        JokeImgLastFragment fragment = new JokeImgLastFragment();
        fragment.setArguments(args);
        return fragment;
    }

    public JokeImgLastFragment() {
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        httpUtil = new HttpUtil(this);
        mList = new ArrayList<>();
        mContext = getContext();
        index = 1;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_joke_img_last, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        lvImg = (ListView) view.findViewById(R.id.lv_img);
        footview = LayoutInflater.from(mContext).inflate(R.layout.footview, null, false);
        adapter = new LvAdapter<>(mList, BR.imgbean, R.layout.joke_img_fragment, mContext);
        lvImg.setAdapter(adapter);
        httpUtil.downLoad(StringUrl.getLastInterImg(index,20));
        setListener();
    }

    private void setListener() {
        lvImg.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                JokeImgBean.ResultBean.DataBean dataBean = (JokeImgBean.ResultBean.DataBean) adapterView.getItemAtPosition(i);
                if (DataChange.isGif(dataBean.getUrl())){
                    Glide.with(mContext).load(dataBean.getUrl()).asGif().into((ImageView) view.findViewById(R.id.iv_joke));
                    view.findViewById(R.id.iv_gif).setVisibility(View.GONE);
                } else {
                    Intent intent = new Intent(mContext, ImageActivity.class);
                    intent.putExtra("path",dataBean.getUrl());
                    Logger.i(dataBean.getUrl());
                    startActivity(intent);
                }
            }
        });
        lvImg.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView absListView, int i) {
                switch (i) {
                    case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:// 是当屏幕停止滚动时
                        // 判断滚动到底部
                        if (lvImg.getLastVisiblePosition() == (lvImg
                                .getCount() - 1)) {
                            if (!hasShow){
                                index++;
                                lvImg.addFooterView(footview);
                                hasShow = !hasShow;
                                httpUtil.downLoad(StringUrl.getLastInterImg(index,20));
                            }
                        }
                        // 判断滚动到顶部
                        if (lvImg.getFirstVisiblePosition() == 0) {
                        }

                        break;
                    case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:// 滚动时
                        break;
                    case AbsListView.OnScrollListener.SCROLL_STATE_FLING:// 是当用户由于之前划动屏幕并抬起手指，屏幕产生惯性滑动时
                        break;
                }
            }

            @Override
            public void onScroll(AbsListView absListView, int i, int i1, int i2) {

//                c = lvJokeByTime.getChildAt(0);
//                if (c != null) {
//                    int scrollY = -c.getTop() + lvJokeByTime.getFirstVisiblePosition() * c.getHeight();
////                    Logger.i(c.getTop()+"getTop");
//                }
            }
        });
    }

    @Override
    public void show(String jsonData) {
        JokeImgBean imgBean = new Gson().fromJson(jsonData, JokeImgBean.class);
        mList.addAll(imgBean.getResult().getData());
//        Logger.i(mList.size() + "");
        adapter.setList(mList);
        if (hasShow == true){
            hasShow = false;
            lvImg.removeFooterView(footview);
        }
        Logger.i(adapter.getCount()+"");
    }

    @Override
    public void failure() {

    }
}
